package com.moyu.article.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.moyu.article.domain.board.Board;
import com.moyu.article.domain.board.dto.*;
import com.moyu.article.domain.board.vo.BoardHotVO;
import com.moyu.article.domain.board.vo.BoardVO;
import com.moyu.article.domain.post.vo.BoardOptionVO;
import com.moyu.article.domain.post.vo.PostListVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author 赵彦博
 * @Date 2025/3/29 19:03
 */
@Mapper
public interface BoardMapper extends BaseMapper<Board> {
    List<BoardVO> getBoardList(BoardQueryDTO boardQueryDTO);

    @Select("SELECT MAX(sort) FROM tb_board")
    Integer selectMaxSort();

    int updateBoardService(BoardUpdateDTO boardUpdateDTO);

    @Select("SELECT sort FROM tb_board WHERE board_id = #{boardId}")
    Integer selectSortById(@Param("boardId") Long boardId);

    @Update("UPDATE tb_board SET sort = sort - 1 " +
            "WHERE sort BETWEEN #{start} AND #{end} " +
            "AND board_id != #{excludeId}")
    int decreaseSortBetween(@Param("start") int start,
                            @Param("end") int end,
                            @Param("excludeId") Long excludeId);

    @Update("UPDATE tb_board SET sort = sort + 1 " +
            "WHERE sort BETWEEN #{start} AND #{end} " +
            "AND board_id != #{excludeId}")
    int increaseSortBetween(@Param("start") int start,
                            @Param("end") int end,
                            @Param("excludeId") Long excludeId);

    int batchUpdateSort(@Param("list") List<BatchSortUpdateDTO> updateList);

    @Select("<script>" +
            "SELECT COUNT(*) FROM tb_board WHERE board_id IN " +
            "<foreach item='id' collection='list' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    Long selectCountByIds(@Param("list") List<Long> ids);

    @Select("SELECT COUNT(*) FROM tb_post WHERE board_id = #{boardId}")
    int countAssociatedPosts(Long boardId);

    List<BoardOptionVO> selectBoardOptions();

    List<BoardVO> getAllBoard(BoardAllQueryDTO dto);

    List<BoardHotVO> getHotBoard();

    List<PostListVO> listByBoardId(String boardId);

    BoardVO detailByBoardId(String boardId);
}
